// 通用方法
/**
 * @description 对象转url参数,只支持简单数据类型
 * @param {Object}  params 要转换成url参数的对象，例如:{a:1,b:2}
 * @return {String} 返回的为?a=1&b=2
 */

export function objToStr(params = {}) {
  let arr = Object.keys(params);
  let str = "?";
  for (var i = 0; i < arr.length; i++) {
    str += arr[i] + "=" + params[arr[i]] + "&";
  }
  return str.slice(0, str.length - 1);
}

/**
 * @description 将url?后面的参数转成对象
 * @alias url 别名
 * @param {String} url url字符串
 * @returns {object}
 */

export function urlToObj(url) {
  if (!url) return {};
  let dourl = decodeURIComponent(url);
  let arr = dourl.split("?")[1].split("&");
  let obj = {};
  for (var i = 0; i < arr.length; i++) {
    let key = arr[i].split("=")[0];
    let value = arr[i].split("=")[1];
    obj[key] = value;
  }
  return obj;
}

/**
 * @description 表单验证未填写
 * @param {Object}  obj 要上传的对象
 * @param {Array}  tipsList 验证未填写的内容 { name: '对象名称', tips: '未填写时提示信息'}
 * @return {Boolean}  如没有未填写内容没有返回值 反之,返回true 可在写上: let res = c_notFilled(obj,list)
 * 																	if(res){return}
 *															来结束后续操作
 */
export function notFilled(obj, tipsList) {
  for (let key in obj) {
    // console.log(key);
    // console.log(obj[key]);
    let err = null;
    if (!obj[key] || obj[key].length < 1) {
      tipsList.forEach((v) => {
        if (v.name == key) {
          err = v.tips;
          console.log("err", err);
        }
      });
    }
    if (err) {
      uni.showToast({
        title: err,
        icon: "none",
      });
      return true;
    }
  }
}

/**
 * @description 表单验证未填写
 * @param {Object}  obj 要上传的对象
 * @param {Array}  tipsList 验证未填写的内容 { name: '对象名称', tips: '未填写时提示信息'}
 * @return {String}
 *
 */
export function phoneEn(phone) {
  let reg = /(\d{3})\d{4}(\d{4})/; // 正则表达式匹配电话号码
  let encryptedPhone = phone.replace(reg, "$1****$2");
  console.log("encryptedPhone", encryptedPhone);
  return encryptedPhone; // 输出加密后的电话号码
}

export function urlTobase64(fileName, format = "png") {
  let img = `${fileName}`,
    imgBase64 = wx.getFileSystemManager().readFileSync(img, "base64"),
    base64Url = `data:image/png;base64,${imgBase64}`;
  /* console.log(
	"data:image/png;base64," + imgBase64,
	"图片转换成功+++++++++++++++++++++++++++++"
	); */

  return base64Url;
}
